/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package poetry.gui

import scala.swing._
import event._
import poetry.parser._
import FileChooser._

object Main extends SimpleSwingApplication {
  override def startup(args: Array[String]) = {
    val t = top
    t.pack
    t.location = new Point(300,100)
    t.size = new Dimension(550,450)
    t.visible = true
    if (args.length >= 1) loadFile(args(0),14)
    else body.contents = new MainComponent(14,true)
  }

  def top = new MainFrame {
    title = "პოეზია"
    menuBar = mBar
    contents = body
    this.peer.setDefaultCloseOperation(2) //DISPOSE_ON_CLOSE
    listenTo(PoetryPublisher)
    reactions += {
      case ChangesNotSaved => notSaved = true
      case ChangesSaved => notSaved = false
    }

    override def dispose = if (notSaved) {
      (new MyConfirm("თქვენ არ შეინახეთ ცვლილებები ფაილში. გნებავთ პროგრამის დახურვა?")).open
    } else System.exit(0)

    override def closeOperation = {
      visible = true
      dispose
    }
  }

  def fOpen {
    val e = chooser.showOpenDialog(mBar)
    e match {
      case Result.Cancel =>
      case _ => loadFile(chooser.selectedFile.getPath,14)
    }
  }

  def fSave {
    val e = chooser.showSaveDialog(mBar)
    e match {
      case Result.Cancel =>
      case _ => saveFile(chooser.selectedFile.getPath)
    }
  }

  def fExit = if (notSaved) {
    (new MyConfirm("თქვენ არ შეინახეთ ცვლილებები ფაილში. გნებავთ პროგრამის დახურვა?")).open
  } else System.exit(0)

  def pNew(fSize: Int) =
    body.contents = new MainComponent(fSize, true)

  def pEdit(component: MainComponent) =
    body.contents = new MainComponent(component.data, component.fontSize, true)

  def pSave(component: MainComponent)  = if (component.editable) {
    val poetry = component.toPoetry
    if (poetry.name.length == 0 || poetry.bodyString.replaceAll("\n", "").length == 0)
      (new MyMessage("ლექსი არასრულია! შეავსეთ ყველა ველი!")).open
    else {
      db.poetry = poetry::db.poetry
      PoetryPublisher.publish(ChangesNotSaved)
      PoetryPublisher.publish(PoetryDbChanged)
      load(poetry)
    }
  }

  def pDelete(component: MainComponent) = {
    val poetry = component.data
    val tmp = db.poetry.find( x => x == poetry)
    if (tmp == None) (new MyMessage("ლექსი ბაზაში არ მოიძებნა!")).open
    else {
      val tmp = db.poetry
      db.poetry = tmp.remove( x => x == poetry)
      body.contents = new MainComponent(component.fontSize, true)
      PoetryPublisher.publish(ChangesNotSaved)
      PoetryPublisher.publish(PoetryDbChanged)
      (new MyMessage("ლექსი წაშლილია!")).open
    }
  }

  def zoomIn: Unit = {
    val content = body.getContent
    content.fontSize * 3 / 2 match {
      case j: Int if j > 200 =>
      case j: Int => load(content,j)
    }
  }

  def zoomOut: Unit = {
    val content = body.getContent
    content.fontSize / 3 * 2 match {
      case j: Int if j < 10 =>
      case j: Int => load(content,j)
    }
  }

// Used by startup and open dialog
  def loadFile(path: String, fontSize: Int): Unit = {
    body.cursor = new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR)
    val content = PoetryXMLParser.readFile(path)
    if (content != None ) body.contents = new MainComponent(content.get, fontSize, false)
    body.cursor = java.awt.Cursor.getDefaultCursor
  }

// Used by Save menu
  def saveFile(path: String): Unit = {
    body.cursor = new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR)
    PoetryXMLParser.writeFile(path)
    body.cursor = java.awt.Cursor.getDefaultCursor
  }

// Used for Zooming
  def load(component: MainComponent, fontSize: Int): Unit = {
    body.cursor = new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR)
    body.contents = new MainComponent(component.data, fontSize, component.editable)
    body.cursor = java.awt.Cursor.getDefaultCursor
  }

// Used for Poetry menu
  def load(poetry: Poetry): Unit = {
    body.cursor = new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR)
    val fSize = body.getContent.fontSize
    body.contents = new MainComponent(poetry, fSize, false)
    body.cursor = java.awt.Cursor.getDefaultCursor
  }

  val mBar: MenuBar = new MenuBar() {
    import java.awt.event.{KeyEvent, ActionEvent => JActionEvent}

    focusable = true
    contents += new MyMenu("ფაილი") {
      mnemonic = Key.F
      contents += new MyMenuItem(Action("გახსნა...") { fOpen }) {
        mnemonic = Key.O
        accelerator = (KeyEvent.VK_O, JActionEvent.CTRL_MASK)
      }
      contents += new MyMenuItem(Action("შენახვა...") { fSave }) {
        mnemonic = Key.W
        accelerator = (KeyEvent.VK_W, JActionEvent.CTRL_MASK)
      }
      contents += new Separator
      contents += new MyMenuItem(Action("გამოსვლა") { fExit }) {
        mnemonic = Key.E
      }
    }
    contents += new MyMenu("რედაქტირება") {
      mnemonic = Key.F
      contents += new MyMenuItem(Action("ლექსის დამატება") { pNew(body.getContent.fontSize) }) {
        mnemonic = Key.N
        accelerator = (KeyEvent.VK_N, JActionEvent.CTRL_MASK)
      }
      contents += new MyMenuItem(Action("ლექსის რედაქტირება") { pEdit(body.getContent) }) {
        mnemonic = Key.E
        accelerator = (KeyEvent.VK_E, JActionEvent.CTRL_MASK)
      }
      contents += new Separator
      contents += new MyMenuItem(Action("ლექსის დამახსოვრება") { pSave(body.getContent) }) {
        mnemonic = Key.S
        accelerator = (KeyEvent.VK_S, JActionEvent.CTRL_MASK)
      }
      contents += new MyMenuItem(Action("ლექსის წაშლა") { pDelete(body.getContent) }) {
        mnemonic = Key.D
        accelerator = (KeyEvent.VK_D, JActionEvent.CTRL_MASK)
      }
    }
    contents += new MyMenu("ხედი") {
      mnemonic = Key.V
      contents += new MyMenuItem(Action("ფონტის გაზრდა") { zoomIn }) {
        accelerator = (KeyEvent.VK_PAGE_UP, JActionEvent.ALT_MASK)
      }
      contents += new MyMenuItem(Action("ფონტის შემცირება") { zoomOut }) {
        accelerator = (KeyEvent.VK_PAGE_DOWN, JActionEvent.ALT_MASK)
      }
      contents += new Separator
      contents += new MyMenu("ლექსები") {
        mnemonic = Key.P
        listenTo(PoetryPublisher)
        reactions += {
          case PoetryDbChanged =>
            val l = db.poetry
            contents.clear
            for (i <- l) contents += new MyMenuItem(Action(i.name) { load(i) })
        }
      }
    }
    contents += new MyMenu("დახმარება") {
      mnemonic = Key.H
      contents += new MyMenuItem(Action("ინსტრუქცია") { Instruction.open }) {
        mnemonic = Key.I
      }
      contents += new MyMenuItem(Action("პოეზია-ს შესახებ") { About.open }) {
        mnemonic = Key.A
      }
    }
  }

  val body = new MyScrollPane
  val db = PoetryXMLParser.poetryDb
  private val chooser = new FileChooser
  private var notSaved = false
}
